package com.futao.springbootlearn20220224.filterdemo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Spring提供的确保只执行一次的Filter，内部是通过设置request.attribute实现的
 *
 * @author futao@gmail.com
 * @date 2022/3/28
 */
@Slf4j
@Component
public class OnceFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        log.info("确保只执行一次-开始");
        filterChain.doFilter(request, response);
        log.info("确保只执行一次-结束");
    }
}
